<?php


/** ensure this file is being included by a parent file */
defined('_JEXEC') or die('Restricted access');

	// TODO Move this to view.html.php
		$r = $this->row;
		// date stuff
		$date = HelpDeskConfig::getDate();
		$today = HelpDeskHTML::date($date->toMysql(), "%Y-%m-%d");
		$yesterday_time	= $date->toUnix() - (24 * 60 * 60); // timestamp - 24 hours * 60 mins * 60secs
		$yesterday		= HelpDeskHTML::date($yesterday_time, "%Y-%m-%d");
		
		// ticket-specific
		$last_modified 	= HelpDeskHTML::date($r->last_modified_datetime, "%a, %d %b %Y");
		$created 		= HelpDeskHTML::date($r->created_datetime, "%a, %d %b %Y");
		
        $responses = count($this->messages);
?>

		<?php
		echo "<form action='{$this->formAction}' method='post' name='adminForm' id='adminForm' enctype='multipart/form-data'>";
		?>

		<table class="adminheading">
		<tr>
            <th class='helpdesk_ticket'>
            <?php echo HelpDeskText::_( 'View Ticket' ); ?>
            </th>
            <?php if (intval($this->surrounding["prev"] > 0)) { ?>
            	<td class="right">
            	<a href='index2.php?option=com_helpdesk&controller=tickets&task=view&cid[]=<?php echo $this->surrounding["prev"]; ?>'>
	            <img src="<?php echo HelpDeskUrl::root(); ?>/components/com_helpdesk/images/skip_back_48.png" border="0"/>
                </a>
            	</td>			
            <?php } ?>
            <?php if (intval($this->surrounding["next"] > 0)) { ?>
            	<td class="right">
            	<a href='index2.php?option=com_helpdesk&controller=tickets&task=view&cid[]=<?php echo $this->surrounding["next"]; ?>'>
	            <img src="<?php echo HelpDeskUrl::root(); ?>/components/com_helpdesk/images/skip_forward_48.png" border="0"/>
                </a>
            	</td>			
            <?php } ?>

		</tr>
		</table>

		<table class="invisible">
			<tr>
				<td width="70%" valign="top">   
					
					<div id='onBeforeDisplay_wrapper'>
						<?php 
							$dispatcher =& HelpDeskHelperPlugin::getInstance();
							$dispatcher->trigger( 'onBeforeDisplayTicket', array( $this->row, $this->user ) );
						?>
					</div>
					
					<div id='subj_desc'>
						<table class="userlist">
							<thead>
								<tr>
									<th>
										<?php echo HelpDeskText::_( 'Subject' ); ?>
										+
										<?php echo HelpDeskText::_( 'Description' ); ?>
									</th>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td>
										<strong><?php echo HelpDeskText::_( 'Subject' ); ?>: </strong>
										<?php echo stripslashes( $r->subject ); ?>
									</td>
								</tr>
								<tr>
									<td>
										<?php echo nl2br( stripslashes( $r->description ) ); ?>									
									</td>
								</tr>
							</tbody>
					        <tbody>
					    </table>
					</div>

						<table class="userlist">
							<thead>
								<tr>
									<th>
										<?php echo HelpDeskText::_( 'Discussion' ); ?>
										<span class="href" id="showhideaddcomment" onclick="displayDiv ('addcomment', 'showhideaddcomment', '<?php echo HelpDeskText::_( "Add Comment" ); ?>', '<?php echo HelpDeskText::_( "Add Comment" ); ?>');">
											<?php echo HelpDeskText::_( "Add Comment" ); ?>
										</span>
									</th>
								</tr>
							</thead>
						</table>
			        
		                <div id="addcomment" style="display: none">
			                <table class="userlist">
			                <tr>
			                  <td class="input">
								<?php 
									$commentbox = new stdClass();
									$commentbox->display = '<textarea name="description" class="text_area" rows="10" width="98%" style="width:98%" ></textarea>';
									$dispatcher =& HelpDeskHelperPlugin::getInstance();
									$dispatcher->trigger( 'onBeforeDisplayCommentBox', array( &$commentbox, $this->row, $this->user ) );
									echo $commentbox->display;
								?>
			                  </td>
			                </tr>
			                <tr>
			                  <td class="input">
			                  	<input type='button' class='button' onclick="submitform('addcomment')" value='<?php echo HelpDeskText::_( 'Add Comment' ); ?>' />
			                  </td>
			                </tr>
			                </table>
		                </div>
			       
			          <table class="userlist">
			            <?php
			            if ($this->messages) { foreach ($this->messages as $m) {
			                echo "<tr>";
			                echo "<td>";					
								$m->authorimage = "<img src='".HelpDeskUrl::root()."/components/com_helpdesk/images/comment.png'>";
								$dispatcher	=& HelpDeskHelperPlugin::getInstance();
								$dispatcher->trigger('onBeforeDisplayCommentAuthorImage', array( &$m, $this->row, $this->user ) );
							  	echo $m->authorimage;
			                echo "</td>";					
			                echo "<td>";
							  $name_display = $this->config->get( 'display_name', '1');
							  if ($name_display == '3') { $name = $m->email; } elseif($name_display == '2') { $name = $m->username; } else { $name = $m->name; }
			                  echo "<strong>$name</strong>";
							  echo " (".HelpDeskHTML::date($m->datetime, "%a, %d %b %Y, %I:%M%p")."):<br>";
								echo "<div class='helpdesk_paddedl'>";
								echo nl2br( stripslashes( $m->message ) );
								echo "</div>";
			                echo "</td>";
			                echo "</tr>";
						} } else {
			                echo "<tr>";
			                echo "<td>";					
							  echo HelpDeskText::_( 'None' );
			                echo "</td>";
			                echo "</tr>";
						}
			            ?>
			          </table>

				</td>

				<td width="30%" valign="top">
					
					<div id='default_data'>
						<table class="userlist">
							<thead>
								<tr>
								<th colspan='2'><?php echo HelpDeskText::_( 'Ticket Info' ); ?></th>
								</tr>
							</thead>
					        <tbody>
								<?php
								  	echo "<tr>";
										echo "<td class='title'>".HelpDeskText::_( 'User' ).":</td>";
										echo "<td>";
											echo "<center>";
												$ticketUser = HelpDeskConfig::getUser($r->sender_userid);
												echo $ticketUser->username." ";
												echo "[ ".$r->sender_userid." ]";
											echo "</center>";
										echo "</td>";
								  	echo "</tr>";
								  	echo "<tr>";
										echo "<td class='title'>".HelpDeskText::_( 'Name' ).":</td>";
										echo "<td>";
											echo "<center>";
												echo $ticketUser->name;
											echo "</center>";
										echo "</td>";
								  	echo "</tr>";
								  	
								  	echo "<tr>";
										echo "<td class='title'>".HelpDeskText::_( 'Email' ).":</td>";
										echo "<td>";
											echo "<center>";
												echo $r->sender_email;
											echo "</center>";
										echo "</td>";
								  	echo "</tr>";
								  	
								  	echo "<tr>";
										echo "<td class='title'>".HelpDeskText::_( 'Ticket ID' ).":</td>";
										echo "<td>";
											echo "<center>";
												echo $r->id;
											echo "</center>";
										echo "</td>";
								  	echo "</tr>";
									
								  	echo "<tr>";
										echo "<td class='title'>".HelpDeskText::_( 'Type' ).":</td>";
										echo "<td>";
											echo "<center>";
												echo HelpDeskText::_( $r->categoryname );
											echo "</center>";
										echo "</td>";
								  	echo "</tr>";
									
								  	echo "<tr>";
										echo "<td class='title'>".HelpDeskText::_( 'Last Modified' ).":</td>";
										echo "<td>";
											echo "<center>";
											if ($last_modified == $today) {
												echo HelpDeskHTML::date($r->last_modified_datetime, "%I:%M%p"); // strftime formatting
											} elseif ($last_modified == $yesterday) {
												echo HelpDeskText::_( 'Yesterday' ).", ".HelpDeskHTML::date($r->last_modified_datetime, "%I:%M%p"); // strftime formatting
											} else {
												echo HelpDeskHTML::date($r->last_modified_datetime, "%d %b %Y, %I:%M%p"); // strftime formatting
											}
											echo "</center>";
										echo "</td>";
								  	echo "</tr>";

								  	echo "<tr>";
										echo "<td class='title'>".HelpDeskText::_( 'Created Date' ).":</td>";
										echo "<td>";
											echo "<center>";
											if ($created == $today) {
												echo HelpDeskHTML::date($r->created_datetime, "%I:%M%p"); // strftime formatting
											} elseif ($created == $yesterday) {
												echo HelpDeskText::_( 'Yesterday' ).", ".HelpDeskHTML::date($r->created_datetime, "%I:%M%p"); // strftime formatting
											} else {
												echo HelpDeskHTML::date($r->created_datetime, "%d %b %Y, %I:%M%p"); // strftime formatting
											}
											echo "</center>";
										echo "</td>";
								  	echo "</tr>";

								  	echo "<tr>";
										echo "<td class='title'>".HelpDeskText::_( 'Status' ).":</td>";
										echo "<td>";
											echo "<center>"; 
												echo HelpDeskHelperHelpDesk::getTicketStatusImage($r->status);
												if ($r->closed_datetime > 0) {
													echo "<br />".HelpDeskHTML::date($r->closed_datetime, "%a, %d %b %Y, %I:%M%p");					
												} else {
													echo "<br />".HelpDeskHelperHelpDesk::getTicketStatus($r->status); 
												}
											echo "</center>";
										echo "</td>";
								  	echo "</tr>";

								  	echo "<tr>";
										echo "<td class='title'>".HelpDeskText::_( 'Feedback' ).":</td>";
										echo "<td>";
											echo "<center>"; 
												echo HelpDeskHelperHelpDesk::getFeedbackRatingImage($r->feedback_rating);											echo "</center>";
											echo "</center>";
										echo "</td>";
								  	echo "</tr>";
								?>
					        </tbody>
				        </table>
					</div>

						<?php if (isset($this->fields)) { ?>
						<div id='default_data'>
							<table class="userlist">
								<thead>
								<tr>
									<th colspan='2'><?php echo HelpDeskText::_( 'Additional Info' ); ?></th>
								</tr>
								</thead>
						        <tbody>
									<?php
										foreach ($this->fields as $field) {
										  	echo "<tr>";
												echo "<td class='title'>".HelpDeskText::_( $field->title ).":</td>";
												echo "<td>";
													echo "<center>";
														$name = $field->db_fieldname;
														if (is_object($this->additional_info)) {
															$value = HelpDeskHelperField::displayValue( $field, $this->additional_info->$name );
															echo $value ? stripslashes( $value ) : HelpDeskText::_( 'Null' );
														} else {
															echo HelpDeskText::_( 'Undefined' );
														}
													echo "</center>";
												echo "</td>";
										  	echo "</tr>";	
										}
									?>
						        </tbody>
					        </table>
						</div>
						<?php } ?>

				        <?php if ($this->config->files_enable) { ?>
						<table class="userlist">
							<thead>
							<tr>
								<th colspan="3"><?php echo HelpDeskText::_("Attachments"); ?></th>
							</tr>
							</thead>
							<?php
							if (count($this->attachments) > 0) { 
								$obj = new HelpDeskFile();
								$dir = $obj->getDirectory();
								foreach ($this->attachments as $r) {
									$link_a = "index2.php?option="._THISCOMPONENT."&controller=tickets&task=downloadfile&id=".$this->row->id."&fileid=".$r->id;
									$link_a = HelpDeskRoute::_( $link_a, false );
								    echo "<tr>";
								    echo "<td>";					
									  echo "<img src='".HelpDeskUrl::root()."/components/com_helpdesk/images/attachment_16.png'>";
								    echo "</td>";					
								    echo "<td>";
										if ($fileexists = file_exists( $dir.DS.$r->physicalname)) {
											echo "<a href='".$link_a."'>";
											echo $r->filename;
											echo "</a>";
										} else {
											echo $r->filename;
										}
								    echo "</td>";
								    echo "<td>";
										echo $r->filesize;
								    echo "</td>";
								    echo "</tr>";
								} 
							} else {
							    echo "<tr>";
							    echo "<td>";
							    echo "</td>";
							    echo "<td>";					
								  echo HelpDeskText::_( 'None' );
							    echo "</td>";
							    echo "</tr>";
							}
							?>
						</table>
				        <?php } ?>

					<?php
						$dispatcher	   =& HelpDeskHelperPlugin::getInstance();
						$dispatcher->trigger('onAfterDisplayTicketInfo', array( $this->row, $this->user ) );					
					?>	
					
				</td>

			</tr>   		
		</table>									  
		
		<?php /* TODO Move this to a plugin */ ?>
        <?php if ($this->config->files_enable) { ?>
                        
			<?php
			// display title
			$title = HelpDeskText::_( 'Attachments' );
			
			$html = '<p>
					<table class="userlist">
						<thead>
							<tr>
								<th class="title">'.
								$title
								.'</th>
							</tr>
						</thead>
					  </table>
					  </p>
					  ';
			echo $html;
			?>
            <div class="note"><?php echo HelpDeskText::_( 'Attachment Intro' ); ?></div>
            
            <table>
            <tr>
              <td class="input">
              <input class="text_area" name="userfile" type="file" size="25"/>
              </td>
            </tr>
            <tr>
              <td class="input">
                <input type='button' class='button' onclick="submitform('addfile')" value='<?php echo HelpDeskText::_( 'Upload Attachment' ); ?>' />
              </td>
            </tr>
            </table>

		<?php } ?>

		<?php /* TODO Move this to a plugin */ ?>
		<?php
		// display title
		$title = HelpDeskText::_( 'Change Ticket Category' );
		
		$html = '<p>
				<table class="userlist">
					<thead>
						<tr>
							<th class="title">'.
							$title
							.'</th>
						</tr>
					</thead>
				  </table>
				  </p>
				  ';
		echo $html;
		?>
        <div class="note"><?php echo HelpDeskText::_( 'Change Ticket Category Intro' ); ?></div>

        <table>		
            <tr>
              <td class='title'><?php echo HelpDeskText::_( 'Category' ); ?>:</td>
              <td class="input">
				<?php echo $this->categories; ?>
              </td>
			  <td></td>
              <td class="input">
				<input type='button' class='button' onclick="submitform( 'moveticket' )" value='<?php echo HelpDeskText::_( 'Change Ticket Category' ); ?>' />
              </td>			  
            </tr>
        </table>

		<?php /* TODO Move this to a plugin */ ?>
		<?php
		// display title
		$title = HelpDeskText::_( 'Change Ticket Status' );
		
		$html = '<p>
				<table class="userlist">
					<thead>
						<tr>
							<th class="title">'.
							$title
							.'</th>
						</tr>
					</thead>
				  </table>
				  </p>
				  ';
		echo $html;
		?>
        <div class="note"><?php echo HelpDeskText::_( 'Change Ticket Status Intro' ); ?></div>
        
        <table>		
                <tr>
                  <td class='input'>
                    <input type='button' class='button' onclick="submitform( 'setpending' )" value='<?php echo HelpDeskText::_( 'Pending Resolution' ); ?>' />
                  </td>
                  <td class='input'>
                    <input type='button' class='button' onclick="submitform( 'setfeedback' )" value='<?php echo HelpDeskText::_( 'Requires User Feedback' ); ?>' />
                  </td>
                  <td class='input'>
                    <input type='button' class='button' onclick="submitform( 'setclosed' )" value='<?php echo HelpDeskText::_( 'Closed Ticket' ); ?>' />
                  </td>
                </tr>
        </table>

		<div id='onAfterDisplay_wrapper'>
			<?php 
				$dispatcher = HelpDeskHelperPlugin::getInstance();
				$dispatcher->trigger( 'onAfterDisplayTicket', array( $this->row, $this->user ) );
			?>
		</div>

        <input type="hidden" name="cid[]" value="<?php echo $this->formId; ?>" />
        <input type="hidden" name="prev" value="<?php echo intval($this->surrounding["prev"]); ?>" />
        <input type="hidden" name="next" value="<?php echo intval($this->surrounding["next"]); ?>" />

		<input type="hidden" name="option" value="<?php echo $this->formOption; ?>" />
		<input type="hidden" name="controller" value="<?php echo $this->formController; ?>" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="id" value="<?php echo $this->formId; ?>" />
		<input type="hidden" name="boxchecked" value="" />
		<input type="hidden" name="order" value="<?php echo @$this->order; ?>" />
		<input type="hidden" name="order_dir" value="<?php echo @$this->order_dir; ?>" />
        
		<?php
		echo $this->formValidate;
		echo "</form>";
		?>
        